<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="记录编码" prop="recordCode">
        <el-input
          v-model="queryParams.recordCode"
          placeholder="请输入记录编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划类型" prop="planType">
        <el-select v-model="queryParams.planType" placeholder="请选择计划类型" clearable>
          <el-option
            v-for="dict in dict.type.dv_plan_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编码" prop="machineryCode">
        <el-input
          v-model="queryParams.machineryCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="machineryName">
        <el-input
          v-model="queryParams.machineryName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="未完成" value="未完成" />
          <el-option label="已完成" value="已完成" />
          <el-option label="已过期" value="已过期" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>

    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>



    <el-table border v-loading="loading" :data="recordList">
      <el-table-column label="记录编码" width="140" align="center" prop="recordCode">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['dv:mainten:record:query']"
          >{{scope.row.recordCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="计划类型" align="center" prop="planType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dv_plan_type" :value="scope.row.planType"/>
        </template>
      </el-table-column>
      <el-table-column label="计划名称" align="center" prop="planName" :show-overflow-tooltip="true"/>
      <el-table-column label="设备编码" align="center" prop="machineryCode" :show-overflow-tooltip="true"/>
      <el-table-column label="设备名称" align="center" prop="machineryName" :show-overflow-tooltip="true"/>
      <el-table-column label="品牌" align="center" prop="machineryBrand" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="machinerySpec" :show-overflow-tooltip="true"/>
      <el-table-column label="计划开始时间" align="center" prop="maintenBeginTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" align="center" prop="maintenEndTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '未完成'" type="info">未完成</el-tag>
          <el-tag v-else-if="scope.row.status == '已完成'" type="success">已完成</el-tag>
          <el-tag v-else type="danger">已过期</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dv:mainten:record:edit']"
            v-if="scope.row.status == '未完成'"
          >执行</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备保养记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <!-- 基本信息 -->
      <el-descriptions class="line-table" title="基本信息" :column="2" border>
        <el-descriptions-item label="记录编码:" :span="2">{{ record.recordCode }}</el-descriptions-item>
        <el-descriptions-item label="计划类型:">{{ record.planType }}</el-descriptions-item>
        <el-descriptions-item label="计划名称:">{{ record.planName }}</el-descriptions-item>
        <el-descriptions-item label="设备编码:">{{ record.machineryCode }}</el-descriptions-item>
        <el-descriptions-item label="设备名称:">{{ record.machineryName }}</el-descriptions-item>
        <el-descriptions-item label="品牌:">{{ record.machineryBrand }}</el-descriptions-item>
        <el-descriptions-item label="规格型号:">{{ record.machinerySpec }}</el-descriptions-item>
        <el-descriptions-item label="计划开始时间:">{{ record.maintenBeginTime }}</el-descriptions-item>
        <el-descriptions-item label="计划结束时间:">{{ record.maintenEndTime }}</el-descriptions-item>
        <el-descriptions-item label="状态:">{{ record.status }}</el-descriptions-item>
        <el-descriptions-item label="提交时间:">{{ record.finishTime }}</el-descriptions-item>
        <el-descriptions-item label="备注:" :span="2">{{ record.remark }}</el-descriptions-item>
      </el-descriptions>
      <el-divider v-if="record.recordId !=null" content-position="center">点检/保养项目</el-divider>
      <!-- 项目信息 -->
      <el-table :data="lineList">
        <el-table-column label="项目编码" align="center" prop="subjectCode" />
        <el-table-column label="项目名称" align="center" prop="subjectName" />
        <el-table-column label="项目类型" align="center" prop="subjectType" />
        <el-table-column label="项目内容" align="center" prop="subjectContent" />
        <el-table-column label="标准" align="center" prop="subjectStandard" />
        <el-table-column label="结果" align="center" prop="result">
          <div class="line-result" slot-scope="scope">
            <el-select v-model="scope.row.result" clearable :disabled="optType == 'view'">
              <el-option label="正常" value="正常"/>
              <el-option label="异常" value="异常"/>
            </el-select>
          </div>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark">
          <div class="line-remark" slot-scope="scope">
            <el-input
              class="item_input"
              v-model="scope.row.remark"
              type="textarea"
              size="mini"
              style="width: 100%;"
              :readonly="optType == 'view'"
            ></el-input>
          </div>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType !='view'">保 存</el-button>
        <el-button type="success" @click="Finish" v-if="optType !='view'">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRecord, listRecord, updateRecord } from "@/api/mes/dv/record";
import { listLine } from "@/api/mes/dv/line";

export default {
  name: "MaintenRecord",
  dicts: ['dv_plan_type'],
  data() {
    return {
      optType: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备保养记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordCode: null,
        planName: null,
        planType: null,
        machineryCode: null,
        machineryName: null,
        status: null,
      },
      // 点检保养记录实体
      record: {},
      // 表单校验
      rules: {},
      // 设备保养记录行表格数据
      lineList: [],
      // line查询参数
      lineParams: {
        pageNum: 1,
        pageSize: 10,
        recordId: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备保养记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 查看按钮操作 **/
    handleView(row) {
      const recordId = row.recordId || this.ids;
      getRecord(recordId).then(response => {
        this.record = response.data;
        this.open = true;
        this.title = "查看点检保养任务";
        this.optType = "view";
      });
      this.getLineList(recordId);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const recordId = row.recordId || this.ids
      getRecord(recordId).then(response => {
        this.record = response.data;
        this.open = true;
        this.title = "修改设备保养任务";
        this.optType = "edit";
      });
      this.getLineList(recordId);
    },
    /** 提交按钮 */
    submitForm() {
      if (this.record.recordId != null) {
        this.record.lineList = this.lineList;
        updateRecord(this.record).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      }
    },
    // 完成提交
    Finish() {
      let list = this.lineList;
      for (let i = 0; i < list.length; i++) {
        if (list[i].result == null) {
          this.$modal.msgError("请填写项目结果！");
          return;
        }
      }
      if (this.record.recordId != null) {
        if (this.isTimeBeforeNow(this.record.maintenEndTime)) {
          this.record.status = '已过期';
        }else {
          this.record.status = '已完成';
        }
        this.record.lineList = this.lineList;
        updateRecord(this.record).then(response => {
          this.$modal.msgSuccess("提交成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 查询设备保养记录行列表 */
    getLineList(recordId) {
      this.lineParams.recordId = recordId
      listLine(this.lineParams).then(response => {
        this.lineList = response.rows;
      });
    },
    isTimeBeforeNow(str) {
      const date = new Date(str);
      if (isNaN(date.getTime())) {
        this.$modal.msgError("时间格式错误");
        return false
      }
      const now = new Date();
      now.setHours(0, 0, 0, 0);
      const targetDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
      return targetDate < now;
    },
  }
};
</script>
